/************************************************************
 * M3D_RECT.H                                      02.11.2006
 *
 * Basic rectangle functionality
 ************************************************************/
#ifndef _M3D_RECT_H_
#define _M3D_RECT_H_
//#include "ap\ap_comm.h"

struct m3dRECT {
   float       l, t, r, b;

   m3dRECT        (void) {}
   m3dRECT        (float _l, float _t, float _r, float _b)  { l = _l; t = _t; r = _r; b = _b; }

   void        SetPosSize        (const m2dV &pos, const m2dV &size)    { *this = m3dRECT(pos.x, pos.y, pos.x + size.x, pos.y + size.y); }   
   // provide relative coordinates + absolute rect and func will position this rect inside absolute rect with needed offsets
   void        SetPosInside      (const m3dRECT &absRect, const m3dRECT &ofsRect);

   float       GetWidth          (void) const            { return r - l; }
   float       GetHeight         (void) const            { return b - t; }
   m2dV        GetTopLeft        (void) const            { m2dV v; v.x = l; v.y = t; return v; }
   m2dV        GetSize           (void) const            { m2dV v; v.x = r - l; v.y = b - t; return v; }
   BOOL        IsEmpty           (void) const            { return GetWidth() < M3D_EPSILON || GetHeight() < M3D_EPSILON; }

   m2dV        GetCenter         (void) const            { m2dV v; v.x = (r + l) * 0.5f; v.y = (t + b) * 0.5f; return v;}

   void        Move              (float dx, float dy)    { l += dx; t += dy; r += dx; b += dy; }
   void        Move              (const m2dV &d)         { Move(d.x, d.y); }
   void        Scale             (float s)               { l *= s; t *= s; r *= s; b *= s; }
   // create rect bounding this and point
   void        Extend            (float x, float y);
   // create rect bounding this and rect
   void        Extend            (const m3dRECT &rect);

   // combine coordinates with another rect 
   void        CombineX          (const m3dRECT &rect)   { l = rect.l; r = rect.r; }
   void        CombineY          (const m3dRECT &rect)   { t = rect.t; b = rect.b; }

   BOOL        IsHit             (float x, float y) const   { return x >= l && y >= t && x < r && y < b; }
   BOOL        IsHit             (const m2dV &p) const      { return IsHit(p.x, p.y); }

   BOOL        IsIntersect       (const m3dRECT &r) const;

   bool        operator    ==    (const m3dRECT &a) const      { return m3dIsEqual(l, a.l) && m3dIsEqual(t, a.t) && m3dIsEqual(r, a.r) && m3dIsEqual(b, a.b); }
   bool        operator    !=    (const m3dRECT &a) const      { return !(*this == a); }
};


#endif // _M3D_RECT_H_
